<?php
namespace App\Service\H5;

class WhywService extends BaseService
{
    //列表
    public static function index($pas)
    {
        $arys = ['data' => [], 'delay_str' => ''];
        $daystart = date('Y-m-d', time()) . ' 00:00:01';
        $dayend = date('Y-m-d', time()) . ' 23:59:59';
        $db = self::dbConn(self::$dbYwWh)->where('del', 0);
        $db = $db->where('staff_id', $pas['uid']);
        if ($pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        $db = $db->where('uptime', '>=', $daystart);
        $db = $db->where('uptime', '<=', $dayend);
        $res = $db->get();
//        if (!count($res)) {
//            return self::toback(-2, '数据不存在');
//        }
        $delay_today = 0;
        if (count($res)) {
            foreach ($res as $v) {
                $itemOne = (array) self::dbConn(self::$dbConfigItem)->where('id', $v->item_id)->first();
                $item_name = empty($itemOne) ? '' : $itemOne['title'];
                $taskOne = (array) self::dbConn(self::$dbYwTask)->where('id', $v->task_id)->first();
                $task_name = empty($taskOne) ? '' : $taskOne['title'];
                $itemOnes = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('wh_id', $v->id)
                    ->orderBy('starthour', 'asc')
                    ->get();
                $whList = [];
                $delay_total = 0;
                if (count($itemOnes)) {
                    foreach ($itemOnes as $vv) {
                        $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
                            ->where('id', $vv->config_whid)
                            ->first();
                        $wh_title = empty($configOne) ? '' : $configOne['title'];
                        $cntOne = (array) self::dbConn(self::$dbConfigWhCnt)
                            ->where('id', $vv->cnt_id)
                            ->first();
                        $cnt_title = empty($cntOne) ? '' : $cntOne['cnt'];
                        $whList[] = [
                            'id' => $vv->id, 'title' => $cnt_title,
                            'start' => $vv->starthour, 'end' => $vv->endhour,
                            'delay' => self::shiByFen($vv->delay), 'wh_title' => $wh_title,
                        ];
                        $delay_total += $vv->delay;
                        $delay_today += $vv->delay;
                    }
                }
                $arys['data'][] = [
                    'id' => $v->id, 'item_id' => $v->item_id, 'item_name' => $item_name,
                    'task_id' => $v->task_id, 'task_name' => $task_name, 'whList' => $whList,
                    'in_day' => $v->in_day, 'delay' => self::shiByFen($delay_total),
                    'status' => $v->status,
                ];
            }
        }
        $arys['delay_str'] =  self::shiByFen($delay_today);
        return self::toback(0, '', $arys);
    }

//    //详情
//    public static function getOne($id)
//    {
//        $res = self::dbConn(self::$dbYwWh)
//            ->select('id', 'item_id', 'task_id')
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return self::toback(-2, '没有数据');
//        }
//        $itemAry = self::dbConn(self::$dbConfigItem)
//            ->select('id', 'title')
//            ->where('del', 0)
//            ->get();
//        $taskAry = self::dbConn(self::$dbYwTask)
//            ->select('id', 'title')
//            ->where('del', 0)
//            ->get();
//        //开始时间
//        $arys = [
//            'data' => $res,
//            'itemList' => $itemAry,
//            'taskList' => $taskAry,
//        ];
//        return self::toback(0, '', $arys);
//    }

    //详情
    public static function getOne($id, $pas)
    {
        $res = (array) self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->first();
        $arys['data'] = $res;
        $itemOnes = self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->groupBy('config_whid')
            ->get();
        $whList = [];
        $delay_total = 0;
        if (count($itemOnes) && $res) {
            foreach ($itemOnes as $v) {
                $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
                    ->where('id', $v->config_whid)
                    ->first();
                $title = empty($configOne) ? '' : $configOne['title'];
                $subOnes = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('config_whid', $v->config_whid)
                    ->where('wh_id', $id)
                    ->orderBy('starthour', 'asc')
                    ->get();
                $subList = [];
                $delay_pid = 0;
                if (count($subOnes)) {
                    foreach ($subOnes as $vv) {
                        $cntOne = (array) self::dbConn(self::$dbConfigWhCnt)
                            ->where('id', $vv->cnt_id)
                            ->first();
                        $resDelay = self::getDelay($vv->starthour, $vv->endhour);
                        $subList[] = [
                            'id' => $vv->id,
                            'cnt_id' => $v->cnt_id,
                            'cnt_str' => empty($cntOne) ? '' : $cntOne['cnt'],
                            'starthour' => $vv->starthour,
                            'endhour' => $vv->endhour,
                            'delay' => $vv->delay,
                            'delay_str' => $resDelay['code'] < 0 ? '' : $resDelay['data']['str'],
                        ];
                        $delay_pid += $vv->delay;
                    }
                }
                $whList[] = [
                    'id' => $v->config_whid, 'title' => $title,
                    'config_whid' => $v->config_whid,
                    'config_title' => $title,
                    'child' => $subList,
                    'delay' => self::shiByFen($delay_pid),
                ];
                $delay_total += $delay_pid;
            }
        }
        $arys['data']['whList'] = $whList;
        $arys['data']['delay'] = self::shiByFen($delay_total);
        //项目
        $arys['item'] = self::getItemListByUid($pas['uid'], $pas['depart_id']);
        //任务
        $in_day = isset($arys['data']['in_day'])&&$arys['data']['in_day'] ? $arys['data']['in_day'] : date('Y-m-d', time());
        $arys['task'] = [];
        $taskOnes = self::dbConn(self::$dbYwTask)
            // ->select('id', 'title')
            ->where('del', 0)
            ->where('depart_id', $pas['depart_id'])
            ->where('in_day', $in_day)
            ->get();
        if (count($taskOnes)) {
            foreach ($taskOnes as $v) {
                $departOne = (array) self::dbConn(self::$dbStDepart)
                    ->where('id', $v->depart_id)
                    ->first();
                $depart_name = empty($departOne) ? '' : $departOne['title'];
                $arys['task'][] = [
                    'id' => $v->id, 'title' => $v->title,
                    'in_day' => $v->in_day, 'depart_name' => $depart_name,
                ];
            }
        }
        //录入日期
        $arys['data']['in_day'] = $in_day;
        //默认项目
        $whUidOne = (array) self::dbConn(self::$dbConfigWhUid)
            ->where('del', 0)
            ->where('uid', $pas['uid'])
            ->first();
        $arys['data']['item_default'] = empty($whUidOne) ? 0 : $whUidOne['item_id'];
        $arys['data']['item_id'] = (isset($arys['data']['item_id'])&&$arys['data']['item_id']) ? $arys['data']['item_id'] : 0;
        return self::toback(0, '', $arys);
    }

    // 任务列表
    public static function getTaskList($in_day, $depart_id)
    {
        $db = self::dbConn(self::$dbYwTask)
            // ->select('id', 'title')
            ->where('del', 0);
        if ($in_day) {
            $db = $db->where('in_day', $in_day);
        }
        if ($depart_id) {
            $db = $db->where('depart_id', $depart_id);
        }
        // return $db->get();
        $res = $db->get();
        $arys = [];
        if (!count($res)) {
            return $arys;
        }
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'in_day' => $v->in_day, 'depart_name' => $depart_name,
            ];
        }
        return $arys;
    }

    //通过UID获取项目列表
    public static function getItemListByUid($uid, $depart_id)
    {
        $uOne = (array) self::dbConn(self::$dbStaff)->where('id', $uid)->first();
        if (empty($uOne)) {
            return [];
        }
        if ($uOne['depart_lead'] == 2) {
            return self::dbConn(self::$dbConfigItem)
                ->select('id', 'title')
                ->where('del', 0)
                ->where('depart_id', $depart_id)
                ->where('is_able', 1)
                ->get();
        }
        $uis = self::dbConn(self::$dbConfigTeam)
            ->where('del', 0)
            ->where('staff_id', $uid)
            ->get();
        $itemids = [];
        if (!count($uis)) {
            return [];
        }
        foreach ($uis as $v) {
            $itemids[] = $v->item_id;
        }
        if (!count($itemids)) {
            return [];
        }
        return self::dbConn(self::$dbConfigItem)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('depart_id', $depart_id)
            ->whereIn('id', $itemids)
            ->get();
    }

    //设置工时
    public static function setByItem($id, $pas)
    {
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (!empty($res)) {
            self::dbConn(self::$dbYwWh)->where('id', $id)->update($pas);
            $lastid = $id;
        } else {
            $pas['addtime'] = toDate();
            $lastid = self::dbConn(self::$dbYwWh)->insertGetId($pas);
            self::dbConn(self::$dbYwWhStatus)->insertGetId([
                'wh_id' => $lastid, 'pub_uid' => $pas['staff_id'], 'addtime' => toDate(),
            ]);
        }
        return self::toback(0, '', $lastid);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwWh)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //保存
    public static function save($id, $pas)
    {
        $res = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['status'] = 2;
        $pas['uptime'] = toDate();
        self::dbConn(self::$dbYwWh)->where('id', $id)->update($pas);
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $res['id'], 'pub_uid' => $res['staff_id'],
            'type' => 2, 'addtime' => toDate(),
        ]);
        return self::toback();
    }

    //发布
    public static function pub($id, $pas)
    {
        $res = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['status'] = 3;
        $pas['uptime'] = toDate();
        self::dbConn(self::$dbYwWh)->where('id', $id)->update($pas);
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $res['id'], 'pub_uid' => $res['staff_id'],
            'type' => 3, 'addtime' => toDate(),
        ]);
        return self::toback();
    }
    
   //删除
   public static function del($id)
   {
       $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
       if (empty($res)) {
           return self::toback(-2, '数据不存在');
       }
       $res = self::dbConn(self::$dbYwWh)
           ->where('id', $id)
           ->update(['del' => 1, 'deltime' => toDate()]);
       if (!$res) {
           return self::toback(-1, 'error');
       }
       return self::toback();
   }

    //审核列表
    public static function authList()
    {
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0)
            ->where('status', 3);
        $db = $db->orderBy('id', 'desc');
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)->where('id', $v->item_id)->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)->where('id', $v->task_id)->first();
            $task_name = empty($taskOne) ? '' : $taskOne['title'];
            // $whOne = (array) self::dbConn(self::$dbConfigWorkHour)->where('id', $v->wh_id)->first();
            // $wh_name = empty($whOne) ? '' : $whOne['title'];
            // $delay = (strtotime($v->endhour) - strtotime($v->starthour)) / 60;
            $whOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->orderBy('starthour', 'asc')
                ->get();
            $delay_total = 0;
            $whList = [];
            foreach ($whOnes as $vv) {
                $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                    ->where('id', $vv->config_whid)
                    ->first();
                $wh_name = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                $whList[] = [
                    'id' => $vv->id, 'wh_name' => $wh_name,
                    'start' => $vv->starthour, 'end' => $vv->endhour,
                    'delay' => $vv->delay,
                ];
                $delay_total += $vv->delay;
            }
            $uOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $uname = empty($uOne) ? '' : $uOne['title'];
            $arys[] = [
                'id' => $v->id, 'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                // 'wh_id' => $v->wh_id, 'wh_name' => $wh_name,
                // 'starthour' => $v->starthour, 'endhour' => $v->endhour,
                // 'delay' => $delay.'分钟',
                'whList' => $whList,
                'in_day' => $v->in_day, 'uname' => $uname,
                'delay_total' => $delay_total.'分钟',
            ];
        }
        return self::toback(0, '', $arys);
    }

    //已审核列表
    public static function authOkList()
    {
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0)
            ->whereIn('status', [4,5]);
        $db = $db->orderBy('id', 'desc');
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)->where('id', $v->item_id)->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)->where('id', $v->task_id)->first();
            $task_name = empty($taskOne) ? '' : $taskOne['title'];
            $whOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->orderBy('starthour', 'asc')
                ->get();
            $delay_total = 0;
            $whList = [];
            foreach ($whOnes as $vv) {
                $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                    ->where('id', $vv->config_whid)
                    ->first();
                $wh_name = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                $whList[] = [
                    'id' => $vv->id, 'wh_name' => $wh_name,
                    'start' => $vv->starthour, 'end' => $vv->endhour,
                    'delay' => $vv->delay,
                ];
                $delay_total += $vv->delay;
            }
            $uOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $uname = empty($uOne) ? '' : $uOne['title'];
            $statuss = self::$whStatuss;
            $status_name = array_key_exists($v->status, $statuss) ? $statuss[$v->status] : '';
            $statusOne = (array) self::dbConn(self::$dbYwWhStatus)
                ->where('wh_id', $v->id)
                ->orderBy('id', 'desc')
                ->first();
            $auth_remark = empty($statusOne) ? '' : $statusOne['remark'];
            $arys[] = [
                'id' => $v->id, 'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                'whList' => $whList,
                'in_day' => $v->in_day, 'uname' => $uname,
                'delay_total' => $delay_total.'分钟',
                'status' => $v->status, 'status_name' => $status_name,
                'auth_remark' => $auth_remark,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //审核动作
    public static function authSet($id, $status, $remark)
    {
        $res = (array) self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '数据不存在');
        }
        if ($res['status'] > 3) {
            return self::toback(-2, '状态错误');
        }
        $statusNew = $status==1 ? 4 : 5;
        self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['status' => $statusNew]);
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'type' => $statusNew, 'remark' => $remark, 'addtime' => toDate(),
        ]);
        return self::toback();
    }
}